package com.yzycoc.coc.gf.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yzycoc.coc.gf.info.RobotGroupVipInfo;
import com.yzycoc.coc.gf.constant.ApiUrlConstant;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * vip群聊
 * 创建时间： 2025/3/3
 *
 * @author yzy
 * @version 1.0
 **/
@Slf4j
public class VipConfig {

    /**
     * 强制vip版
     */
    public static Boolean SYSTEM_STATE = false;

    /**
     * 联系方式
     */
    public static String CONTACT_INFORMATION = "\nQ:936642284\nV:cscoc666";

    /**
     * 群聊vip
     * key：群号
     * value：会员信息
     */
    public static Map<String, RobotGroupVipInfo> robotGroupVip = new HashMap<>();

    /**
     * 更新会员信息
     */
    public static void startGroupAvgVip() {
        List<RobotGroupVipInfo> robotGroupList = getRobotGroupVipList();
        if (CollUtil.isEmpty(robotGroupList)) {
            return;
        }
        VipConfig.robotGroupVip = robotGroupList.stream().collect(Collectors.toMap(RobotGroupVipInfo::getGroupId, a -> a, (a, b) -> a));
    }

    /**
     * 判断是否是管理员
     */
    public static boolean isVip(String groupId){
        RobotGroupVipInfo robotGroupVipInfo = robotGroupVip.get(groupId);
        if(robotGroupVipInfo == null){
            return false;
        }
        return robotGroupVipInfo.getEndTime() > System.currentTimeMillis();
    }

    /**
     * 免广告的群
     */
    public static List<RobotGroupVipInfo> getRobotGroupVipList() {
        try {
            String body = HttpRequest.post(CocConfig.API_URL + ApiUrlConstant.GROUP_VIP_LIST)
                    .form("type", "0")
                    .form("minTime", System.currentTimeMillis())
                    .header(ApiUrlConstant.TOKEN_AUTHENTICATION, CocConfig.AUTH)
                    .timeout(250000).execute().body();
            log.debug("vip.http body:{}", body);
            JSONObject result = JSON.parseObject(body);
            if (result == null) {
                return new ArrayList<>();
            }
            JSONArray data = result.getJSONArray("result");
            if (CollUtil.isEmpty(data)) {
                return new ArrayList<>();
            }
            return data.toJavaList(RobotGroupVipInfo.class);
        } catch (Exception e) {
            log.error("通讯异常:", e);
        }
        return new ArrayList<>();
    }
}
